package com.shd.edanpei.customer.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.ui.activity.SureLoanInfoActivity;
import com.shd.edanpei.base.TranBarActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.customer.adapter.COrderAdapter;
import com.shd.edanpei.customer.contract.CMainContract;
import com.shd.edanpei.customer.presenter.CMainPresenter;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.util.AcCollector;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.view.MySpinner;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/27.
 * yang-wen-wen
 */

public class CMainActivity extends TranBarActivity<CMainPresenter>
        implements CMainContract.CMainIView {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
//    @BindView(R.id.et_search)
//    EditText etSearch;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.mySpinner)
    MySpinner mySpinner;

    private COrderAdapter mAdapter;
    private Bundle bundle = new Bundle();

    @Override
    protected int setLayout() {
        return R.layout.activity_cmain;
    }

    @Override
    protected CMainPresenter bindPresenter() {
        return new CMainPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new COrderAdapter(this);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.null_layout_c, null));

        UserBean userBean = DaoManager.getInstance().getUserBean();
        tvAccount.setText(userBean.getAccountNo());
        tvName.setText(userBean.getRealName() + "用户");

    }

    @Override
    protected void initListener() {
//        etSearch.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    if (etSearch.getText().toString().isEmpty()) {
//                        return true;
//                    } else {
//                        mPresenter.search(etSearch.getText().toString());
//
//                    }
//                }
//                return false;
//            }
//        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.putInt(Constant.INDEX, position);
                bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
                bundle.putInt(Constant.TYPE, mAdapter.getData().get(position).getUserStatus());
                startActivity(COrderDetailsActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.clear();
                bundle.putInt(Constant.INDEX, position);
                bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
                bundle.putInt(Constant.TYPE, mAdapter.getData().get(position).getUserStatus());
                bundle.putInt(Constant.ORDER_TYPE, mAdapter.getItem(position).getOrderType());
                if (view.getId() == R.id.tv_pay) {
                    //支付押金
//                    startActivity(PayActivity.class, bundle);
                    mPresenter.checkAuthorization(mAdapter.getData().get(position),bundle);

                } else if (view.getId() == R.id.tv_payService) {
                    //填写信息，支付服务费
                    startActivity(SureLoanInfoActivity.class, bundle);
                } else if (view.getId() == R.id.tv_sure) {
                    mPresenter.sureGetLone(mAdapter.getItem(position).getOrderId(),
                            mAdapter.getItem(position).getOrderType(),
                            position);
                }
            }
        });
        mySpinner.setSelectListener(new MySpinner.Listener() {
            @Override
            public void select(String str) {
                if (!TextUtils.isEmpty(str)){
                    mPresenter.search(str);
                }
            }
        });
    }

    @OnClick(R.id.iv_user)
    void onUser() {
        startActivity(CUserActivity.class);
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return null;
    }

    @Override
    public COrderAdapter getAdapter() {
        return mAdapter;
    }

    //刷新列表
    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        mAdapter.getData().get(bean.getIndex()).setUserStatus(bean.getOrderStatus());
        mAdapter.notifyItemChanged(bean.getIndex());
    }

    private final int INTERVAL_TIME = 2000;
    //最近点击返回键时间
    private long lastClickBackTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - lastClickBackTime) > INTERVAL_TIME) {
                ToastUtil.showToast("再按一次退出应用");
                lastClickBackTime = System.currentTimeMillis();
            } else {
//                //退出APP
                AcCollector.finishAll();
                finish();
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
